package com.group3.hospital.controller;

import com.group3.hospital.bean.HealthKnows;
import com.group3.hospital.service.HealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 尚硅谷
 * @create 2021/12/30
 */
@Controller
@SessionAttributes(names = {"healthKnows","pages","oneHealth"})
public class HealthController {
    @Autowired
    private HealthService healthService;

    //第一次查看，把数据库数据显示出来前六个
    @RequestMapping("healthUi")
    public String healthUi(Model model){
        Integer counts = 0;
        List<HealthKnows> healthKnows = new ArrayList<>();
        try {
            healthKnows = healthService.queryAll();
            counts = healthService.queryCount();
//            for (HealthKnows healthKnow : healthKnows) {
//                System.out.println(healthKnow);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer num = counts/6 + 1;
        int[] pages = new int[num];
        model.addAttribute("pages",pages);
        model.addAttribute("healthKnows",healthKnows);
        return "/html/health";
    }

    //后他管理画面第一次进入
    @RequestMapping("healthManagerUi")
    public String healthManagerUi(Model model){
        Integer counts = 0;
        List<HealthKnows> healthKnows = new ArrayList<>();
        try {
            healthKnows = healthService.queryAll();
            counts = healthService.queryCount();
//            for (HealthKnows healthKnow : healthKnows) {
//                System.out.println(healthKnow);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer num = counts/6 + 1;
        int[] pages = new int[num];
        model.addAttribute("pages",pages);
        model.addAttribute("healthKnows",healthKnows);
        return "/html/healthManager";
    }

    @RequestMapping("queryByPage2")
    public String queryByPage2(Integer page,Model model){
        List<HealthKnows> healthKnows = new ArrayList<>();
        try {
            healthKnows = healthService.queryByPage(page * 6);
//            for (HealthKnows healthKnow : healthKnows) {
//                System.out.println(healthKnow);
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("healthKnows",healthKnows);
        return "/html/health";
    }

    //后台管理删除数据
    @RequestMapping("deleteById")
    public String deleteById(Integer id){
        System.out.println(id);

        try {
            healthService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/healthManagerUi";
    }



    //后台管理分页
    @RequestMapping("queryByPage3")
    public String queryByPage3(Integer page,Model model){
        List<HealthKnows> healthKnows = new ArrayList<>();
        try {
            healthKnows = healthService.queryByPage(page * 6);
//            for (HealthKnows healthKnow : healthKnows) {
//                System.out.println(healthKnow);
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("healthKnows",healthKnows);
        return "/html/healthManager";
    }




    //查看没一页健康科普的具体内容 th:href="@{/selectById(id=${selectByHealthId.healthId})}"
    @RequestMapping("selectByHealthId")
    public String selectById(Integer id,Model model){
        HealthKnows healthKnows = new HealthKnows();
        try {
            healthKnows = healthService.selectByHealthId(id);
            System.out.println(healthKnows);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("oneHealth",healthKnows);
        return "/html/healthEveryOne";
    }



    //新增健康科普
    @RequestMapping("addHealthUi")
    public String addHealthUi(){
        return "/html/addHealth";
    }

    @RequestMapping("add")
    public String add(){
        return "redirect:/addHealthUi";
    }


    //实现增加操作
    @RequestMapping("addHealthKnows")
    public String addHealthKnows(String title, String body, MultipartFile img){
        String uuid = UUID.randomUUID().toString().replace("-","");
//        System.out.println(uuid);
        String fileName = img.getOriginalFilename().substring(img.getOriginalFilename().lastIndexOf("."));
//        System.out.println(fileName);
        String newFileName = uuid + fileName;
//        System.out.println(newFileName);
        File file = new File("F:\\images\\"+newFileName);
        try {
            img.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Date date = new Date();
        HealthKnows healthKnows = new HealthKnows();
        healthKnows.setHealthDate(date);
        healthKnows.setHealthTitle(title);
        healthKnows.setHealthBody(body);
        healthKnows.setHealthImg(newFileName);
        try {
            healthService.addHealthKnow(healthKnows);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:healthManagerUi";
    }

}
